<script setup="stafflist">
import staffHttp from '@/api/staffHttp';
import OAMain from '@/components/OAMain.vue';
import timeFormatter from '@/utils/timeFormatter';
import {  ref, reactive, onMounted, watch } from 'vue';
import { ElMessage, paginationEmits } from 'element-plus';


// 分页
let pagination = reactive({
    page:1,
    total:0
})

let page_size = ref(10) //控制页码数量
let staffs = ref([])


// 0、获取员工列表 核心函数
async function fetchStaffList(page,page_size){
    try{
        let data = await staffHttp.getStaffList(page,page_size)
        pagination.total = data.count  // 从服务器返回
        //pagination.page = page        v-model:currentPage 自动更新
        staffs.value = data.results     // 将 后端的返回值 传递给 staffs 数组
    }catch(detail){
        ElMessage.error(detail)
    }
}

// 1、待所有组件都加载完毕后 在去加载 分页
onMounted( ()=>{
    console.log("onMounted 请求")
    fetchStaffList(1,page_size.value)  // 调用 0
})

// 2、监听用户输入的 页码

watch(()=> pagination.page,(newPage)=>{
    fetchStaffList(newPage,page_size.value)
 })

watch(page_size,function(value){
    if(pagination.page == 1){
        fetchStaffList(1,value)
    }else{
        paginationEmits.page = 1
    }
})

</script>

<template>
    <OAMain title="员工列表">
        <el-card>
            <el-table :data="staffs" ref="tableRef">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column label="序号" width="60">
                    <template #default="scope">{{ scope.$index + 1 }}</template>
                </el-table-column>
                <el-table-column prop="realname" label="姓名"></el-table-column>
                <el-table-column label="董事">
                    <template #default="scope">
                        <el-tag v-if="scope.row.is_superuser == 1" type="success">董事</el-tag>
                        <el-tag v-else-if="scope.row.is_superuser == 0" type="warning">员工</el-tag>
                        <el-tag v-else type="danger">已锁定</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="email" label="邮箱"></el-table-column>
                <el-table-column label="入职时间">
                    <template #default="scope">
                        {{ timeFormatter.stringFromDate(scope.row.date_joined) }}
                    </template>
                </el-table-column>
                <el-table-column prop="department.name" label="部门"></el-table-column>
                <el-table-column label="状态">
                    <template #default="scope">
                        <el-tag v-if="scope.row.status == 1" type="success">正常</el-tag>
                        <el-tag v-else-if="scope.row.status == 2" type="warning">未激活</el-tag>
                        <el-tag v-else type="danger">已锁定</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template #default="scope">
                        <el-button type="primary" icon="Edit" circle @click="onEditStaff(scope.$index)"></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <template #footer>
                <div style="display :flex; justify-content: space-between;">
                    <el-form-item label="每页:">
                        <el-select v-model="page_size" size="small" style="width:100px">
                            <el-option select label="10条/页" :value="10" />   <!--xx条/页 :value=xx-->
                            <el-option label="20条/页" :value="20" />
                        </el-select>
                    </el-form-item>
                    <el-pagination background layout="prev,pager,next" :total="pagination.total"
                        v-model:currentPage="pagination.page" :page-size="page_size" />
                </div>
            </template>
        </el-card>
    </OAMain>
</template>

<style scoped></style>